home *** CD-ROM | disk | FTP | other *** search
/ Java Interactive Reference Guide / Java Interactive Reference Guide.iso / autorun / source.dir / 00114_15.txt < prev    next >
Encoding:
Text File  |  1980-01-11  |  2.8 KB  |  128 lines

  1. import java.lang.*;
  2. import awt.*;
  3. import browser.*;
  4. import nathanw.*;
  5.  
  6. /** 
  7.  * The Tetris class is the applet implementation.
  8.  * Two parameters are read in: X and Y, which
  9.  * specify the size of the playing area.
  10.  * @author: Nathan J. Williams <nathanw@mit.edu>
  11.  */
  12.  
  13. class Tetris extends Applet implements Runnable {
  14.     /** the state of the game  */
  15.     TetrisGame game;
  16.     /** the game loop thread */
  17.     Thread gameRunner;
  18.     /** Size of the display area */
  19.     int xs,ys;
  20.     int x,y;
  21.     ScoreBox box;
  22.     boolean gameOver;
  23.  
  24.     /**
  25.      * init() inherited from Applet
  26.      * Parses attributes, initializes variables, 
  27.      * calls resize() and getfocus().
  28.      */
  29.  
  30.     public void init()
  31.     {
  32.  
  33.     System.out.println("Class string:"+toString());
  34.         
  35.         try {
  36.         x=Integer.parseInt(getAttribute("X"));
  37.         } catch (NullPointerException e) { x=10; }
  38.           catch (NumberFormatException e) { x=10; }
  39.  
  40.         try {
  41.         y=Integer.parseInt(getAttribute("Y"));
  42.         } catch (NullPointerException e) { y=20; }
  43.           catch (NumberFormatException e) { y=20; }
  44.          
  45.         game=new TetrisGame(x,y,false);
  46.     box=new ScoreBox("Score: ");
  47.     xs=x*10+16;  // Play area
  48.         ys=y*10+4   // Play area
  49.        +60;    // Score box
  50.     resize(xs,ys);
  51.     getFocus();
  52.     }
  53.  
  54.     public void start()
  55.     {
  56.         if(gameRunner==null)
  57.         {
  58.         gameRunner = new Thread(this);
  59.         gameRunner.start();
  60.         }
  61.         }
  62.  
  63.     public void stop()
  64.         {
  65.     if(gameRunner.isAlive())
  66.         gameRunner.stop();
  67.     gameRunner=null;
  68.     }
  69.     
  70.     public void destroy()
  71.     {
  72.     }
  73.    
  74.     public void run()
  75.     {
  76.     gameOver=false;
  77.         while(!gameOver)
  78.         {
  79.         gameOver=game.step();
  80.         repaint();
  81.         Thread.sleep(400);
  82.         }
  83.         }
  84.  
  85.  
  86.     public void paint(Graphics g)
  87.     {
  88.     game.paint(g,2,2);
  89.         box.setScore(game.score());
  90.     box.paint(g,5,ys-40);
  91.     if(gameOver)
  92.         {
  93.         Font tmp=font;
  94.         g.setForeground(Color.red);
  95.         g.setFont(getFont("TimesRoman",Font.BOLD,18));
  96.             g.drawString("Game Over",10,ys/2);
  97.         g.setFont(tmp);
  98.         g.setForeground(Color.black);
  99.         }
  100.     }
  101.  
  102.     public void keyDown(int key) 
  103.         {
  104.     switch ((char)key)
  105.         {
  106.         case 'R': game = new TetrisGame(x,y,false); 
  107.               stop();
  108.                   start();
  109.               break;
  110.             }
  111.     if(!gameOver)
  112.         {
  113.         switch ((char)key)
  114.             {
  115.             case 'h':
  116.             case 'H': game.move_left(); repaint(); break;
  117.             case 'l':
  118.             case 'L': game.move_right(); repaint(); break;
  119.             case 'j':
  120.                    case 'J': game.rotate_ccw(); repaint(); break;
  121.             case 'k':
  122.                 case 'K': game.rotate_cw(); repaint(); break;
  123.             case ' ': game.drop(); repaint(); break;
  124.                 }
  125.             }
  126.         }
  127. }
  128.